package com.csdj.questions.controller;


import com.csdj.questions.common.WebResult;
import com.csdj.questions.common.WebResultMsgCode;
import com.csdj.questions.entity.AttquesComment;
import com.csdj.questions.service.AttquesCommentService;
import com.csdj.questions.utils.SnowflakeIdUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

/**
 * <p>
 * 评论表 (问题) 前端控制器
 * </p>
 *
 * @author 郭成星
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/attques-comment")
public class AttquesCommentController extends  BaseController {
    SnowflakeIdUtils snowflakeIdUtils;

    @Resource
    private AttquesCommentService attquesCommentService;
    /**
     * 根据问题id增加对应评论
     */
    @GetMapping("/insertComment")
      public WebResult UserInsertComment(HttpServletRequest request,
                                         @RequestParam(value = "quesId", required = true) String quesId,
                                         @RequestParam(value = "content", required = true)String content ){
        int uid = getUid(request);
        AttquesComment attquesComment=new AttquesComment();
        attquesComment.setQuesId(quesId);
        attquesComment.setCommentContent(content);
        //评论人id
        attquesComment.setCommentatorId(uid);
        //获取当前日期
        LocalDate localDate = LocalDate.now();
        String pingLunid=quesId+uid+localDate;
        //利用雪花算法设置评论id
        long id=  SnowflakeIdUtils.nextId();
        attquesComment.setAttquesCommentId(id);

        int jg=attquesCommentService.insertComment(attquesComment);
        if(jg>0){
            return  new WebResult(WebResultMsgCode.SUCCESS).putData("result",true);
        }else{
            return new WebResult(WebResultMsgCode.FAIL).putData("result",false);
        }

      }
}
